package com.jsyxdl.k3cloud.listener.handling;

import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hustcad.plm.pdm.flowbusiness.model.entity.FlowObject;
import com.hustcad.plm.pdm.partbom.dto.ChildPartsFiltersDTO;
import com.hustcad.plm.pdm.partbom.dto.PartBomTree;
import com.hustcad.plm.pdm.partbom.dto.QuerySubstituteLinkDTO;
import com.hustcad.plm.pdm.partbom.dto.TyplmUpdateLinkIbaDTO;
import com.hustcad.plm.pdm.partbom.service.TyplmPartBomService;
import com.hustcad.plm.pdm.partbom.service.partalternate.TyplmPdmPartAlternateService;
import com.hustcad.plm.pdm.partbom.vo.*;
import com.jsyxdl.k3cloud.listener.K3CloudHttpService;
import com.jsyxdl.k3cloud.service.ErpWorkedTaskService;
import com.jsyxdl.k3cloud.utils.PlmPartOptionUtils;
import com.jsyxdl.k3cloud.utils.SendErpDataUtils;
import com.jsyxdl.service.ErpPartAlternativeService;
import com.jsyxdl.service.impl.ExtendtyplmPdmPartAlternateServiceImpl;
import com.jsyxdl.service.impl.ExtendPlmPdmPartSubstituteServiceImpl;
import com.ty.basic.entity.org.UserDO;
import com.ty.basic.exception.message.SystemErrorCodeEnum;
import com.ty.basic.session.TyAccountContext;
import org.apache.commons.lang3.ObjectUtils;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 替代方案处理服务
 */
public class AlternativeSolutionHandling {
    public JSONObject productionCall(String check,Map<String, Object> executionType, FlowObject flowObject) throws Exception {

        PlmPartOptionUtils plmPartOptionUtils = SpringUtil.getBean(PlmPartOptionUtils.class);
        TyplmPdmPartAlternateService typlmPartAlternateService = SpringUtil.getBean(TyplmPdmPartAlternateService.class);
        ErpPartAlternativeService erpPartAlternativeService = SpringUtil.getBean(ErpPartAlternativeService.class);
        K3CloudHttpService k3CloudHttpService = SpringUtil.getBean(K3CloudHttpService.class);
        ExtendPlmPdmPartSubstituteServiceImpl hzplmPdmPartSubstituteService = SpringUtil.getBean(
                ExtendPlmPdmPartSubstituteServiceImpl.class);
        ExtendtyplmPdmPartAlternateServiceImpl hzTyplmPdmPartAlternateService = SpringUtil.getBean(
                ExtendtyplmPdmPartAlternateServiceImpl.class);


        String k3cloud = (String) executionType.get("k3cloud");
        String code = (String) executionType.get("code");

        BigInteger oid = flowObject.getRefObjectOid();
        String oType = flowObject.getRefObjectOtype();
        PartDetailsVO partDetailsVO = plmPartOptionUtils.getPartDetailsVO(oid, oType);

        TyplmPartBomService typlmPartBomService = SpringUtil.getBean(TyplmPartBomService.class);
        if (typlmPartBomService != null) {
            com.hustcad.plm.pdm.partbom.dto.ExpandBomTreeByFilterDTO expandBomTreeByFilterDTO = new com.hustcad.plm.pdm.partbom.dto.ExpandBomTreeByFilterDTO();
            expandBomTreeByFilterDTO.setAttrFilterCondition(new ArrayList<>());

            ChildPartsFiltersDTO childPartsFiltersDTO = new ChildPartsFiltersDTO();
            childPartsFiltersDTO.setBaseLineOid("");
            childPartsFiltersDTO.setDate("");
            childPartsFiltersDTO.setExpandCondition("latest");
            childPartsFiltersDTO.setLifecycleStateKey("");
            childPartsFiltersDTO.setPurposeOfBom("");
            childPartsFiltersDTO.setSpareBom("");
            childPartsFiltersDTO.setView(partDetailsVO.getView());
            List<ChildPartsFiltersDTO> list = new ArrayList<>();
            list.add(childPartsFiltersDTO);
            expandBomTreeByFilterDTO.setConditions(list);

            expandBomTreeByFilterDTO.setExpandLevel("N");
            expandBomTreeByFilterDTO.setFatherNumber(partDetailsVO.getObjectNumber());
            expandBomTreeByFilterDTO.setFatherOid(String.valueOf(partDetailsVO.getOid()));
            expandBomTreeByFilterDTO.setFatherOtype(partDetailsVO.getOtype());
            expandBomTreeByFilterDTO.setFilterMode("0");
            expandBomTreeByFilterDTO.setGiveUsesOccurrence("N");
            expandBomTreeByFilterDTO.setIbaColumns(new ArrayList<>());
            expandBomTreeByFilterDTO.setIsApplyRoot(false);
            expandBomTreeByFilterDTO.setIsFilterAttr(false);
            expandBomTreeByFilterDTO.setIsNeedTranEnum(true);
            expandBomTreeByFilterDTO.setIsNestedTree(false);
            expandBomTreeByFilterDTO.setIsQueryIBA(true);

            PartBomTreeByFilterVO result = typlmPartBomService.expandBomTreeByFilterNew(expandBomTreeByFilterDTO,
                                                                                        Boolean.TRUE);
            UserDO userDO = TyAccountContext.getUser();

            Map<String, Object> cmdAlternative = new HashMap<>();
            Map<String, Object> cmdSubstitute = new HashMap<>();

            List<Map<String, Object>> alternativeList = new ArrayList<>();
            List<PartDetailsVO> partDetailsVOList = new ArrayList<>();
            for (int y = 0; y < result.getPartBomTreeList().size(); y++) {
                PartBomTree partBomTree = result.getPartBomTreeList().get(y);
                List<AlternateInfoVO> listAlternates = null;
                List<PartSubstituteVO> listSubstitute = null;
                if (cmdAlternative.containsKey(partBomTree.getObjectNumber()) == false) {
                    List<String> alternateForPartNumbers = new ArrayList<>();
                    alternateForPartNumbers.add(partBomTree.getObjectNumber());
                    //查询全局替代件
                    listAlternates = typlmPartAlternateService.batchQueryAlternates(alternateForPartNumbers);
                    if (listAlternates != null && listAlternates.size() > 0) {
                        cmdAlternative.put(partBomTree.getObjectNumber(), "0");
                    }
                }
                if (cmdSubstitute.containsKey(partBomTree.getObjectNumber()) == false) {
                    //查询局部替代件
                    QuerySubstituteLinkDTO querySubstituteLinkDTO = new QuerySubstituteLinkDTO();
                    if (ObjectUtils.isNotEmpty(userDO) && ObjectUtils.isNotEmpty(userDO.getOid())) {
                        querySubstituteLinkDTO.setUserOid(userDO.getOid());
                    }
                    querySubstituteLinkDTO.setAoid(String.valueOf(partBomTree.getAoid()));
                    querySubstituteLinkDTO.setBnumber(String.valueOf(partBomTree.getObjectNumber()));
                    querySubstituteLinkDTO.setBoid(String.valueOf(partBomTree.getPartoid()));
                    listSubstitute = hzplmPdmPartSubstituteService.queryPartSubstituteLink(querySubstituteLinkDTO);
                    if (listSubstitute != null && listSubstitute.size() > 0) {
                        cmdSubstitute.put(partBomTree.getObjectNumber(), "1");
                    }
                }

                Map<String, Object> alternative = new HashMap<>();
                if ("全局替代方案".equals(k3cloud)) {
                    if (listAlternates != null && listAlternates.size() > 0) {
                        alternative.put("listAlternates", listAlternates);
                    }
                }
                if ("局部替代方案".equals(k3cloud)) {
                    if (listSubstitute != null && listSubstitute.size() > 0) {
                        alternative.put("listSubstitute", listSubstitute);
                    }
                }


                if (alternative.size() > 0) {
                    BigInteger cpartOid = new BigInteger(partBomTree.getPartoid());
                    String cpartType = partBomTree.getPartotype();
                    PartDetailsVO partDetails = plmPartOptionUtils.getPartDetailsVO(cpartOid, cpartType);
                    alternative.put("partDetails", partDetails);
                    if ("已发布".equals(partDetails.getLifecycleState()) == false) {
                        alternativeList.add(alternative);
                    }
                }
            }

            Map<String, Object> materialsType = k3CloudHttpService.getExecutionType("PLBC0001X", "BOM保存");
            materialsType.put("code", "PLBC0001X");
            materialsType.put("k3cloud", "BOM保存");


            //首先创建替代件物料
            for (int i = 0; i < alternativeList.size(); i++) {
                Map<String, Object> alternative = alternativeList.get(i);
                List<AlternateInfoVO> listAlternates = (List<AlternateInfoVO>) alternative.get("listAlternates");
                List<PartSubstituteVO> listSubstitute = (List<PartSubstituteVO>) alternative.get("listSubstitute");
                if (listAlternates != null) {
                    for (int a = 0; a < listAlternates.size(); a++) {
                        AlternateInfoVO alternateInfoVO = listAlternates.get(a);
                        List<Map<String, Object>> erpList = erpPartAlternativeService.getPartDetailsBylinkOid(
                                new BigInteger(alternateInfoVO.getId()));
                        if (erpList.size() > 0) {
                            oid = new BigInteger(String.valueOf(erpList.get(0).get("linkpartoid")));
                            oType = (String) erpList.get(0).get("linkotype");
                            PartDetailsVO linkPartDetailsVO = plmPartOptionUtils.getPartDetailsVO(oid, oType);
                            partDetailsVOList.add(linkPartDetailsVO);
                        }
                    }
                }
                if (listSubstitute != null) {
                    for (int a = 0; a < listSubstitute.size(); a++) {
                        PartSubstituteVO partSubstituteVO = listSubstitute.get(a);
                        List<Map<String, Object>> erpList = erpPartAlternativeService.getPartDetailsBySubstituteLinkOid(
                                new BigInteger(partSubstituteVO.getLinkId()));
                        if (erpList.size() > 0) {
                            oid = new BigInteger(String.valueOf(erpList.get(0).get("linkpartoid")));
                            oType = (String) erpList.get(0).get("linkotype");
                            PartDetailsVO linkPartDetailsVO = plmPartOptionUtils.getPartDetailsVO(oid, oType);
                            partDetailsVOList.add(linkPartDetailsVO);
                        }
                    }
                }
            }

            MaterialsBatchHandling materialsBatchHandling = new MaterialsBatchHandling();
            JSONObject erpResult = materialsBatchHandling.productionCall(check,materialsType, partDetailsVOList);
            if (erpResult.containsKey("Result")) {
                JSONObject Result = erpResult.getJSONObject("Result");
                if (Result.containsKey("ResponseStatus")) {
                    JSONObject ResponseStatus = Result.getJSONObject("ResponseStatus");
                    if (ResponseStatus.getBooleanValue("IsSuccess") == false) {
                        throw SystemErrorCodeEnum.WORK_FLOW_CONTENT_ERROR.getException(
                                ResponseStatus.get("Errors").toString());
                    }
                }
            }

            //创建替代关系
            List<Map<String, Object>> options = k3CloudHttpService.getOptions(code, k3cloud);
            Map<String, Object> inMap = k3CloudHttpService.getInTreeMap(code, k3cloud);
            List<String> plmNumbers = new ArrayList<>();

            for (int i = 0; i < alternativeList.size(); i++) {
                Map<String, Object> alternative = alternativeList.get(i);
                List<AlternateInfoVO> listAlternates = (List<AlternateInfoVO>) alternative.get("listAlternates");
                List<PartSubstituteVO> listSubstitute = (List<PartSubstituteVO>) alternative.get("listSubstitute");
                PartDetailsVO partDetails = (PartDetailsVO) alternative.get("partDetails");

                if (listAlternates != null && listAlternates.size() > 0) {
                    //判断是否需要修改替代件
                    boolean isUpdate = false;
                    for (int a = 0; a < listAlternates.size(); a++) {
                        AlternateInfoVO alternateInfoVO = listAlternates.get(a);

                        Map<String, List<String>> allTd = alternateInfoVO.getIbaAttributeMap();
                        if (allTd != null) {
                            if (allTd.containsKey("替代方案编码")) {
                                List<String> cList = allTd.get("替代方案编码");
                                if (cList.size() == 0 || "".equals(cList.get(0))) {
                                    isUpdate = true;
                                    break;
                                }
                            }
                        } else {
                            isUpdate = true;
                        }
                    }


                    if (isUpdate) {
                        Map<String, Object> bomKeyValue = new HashMap<>();
                        List<String> removeList = new ArrayList<>();
                        removeList.add(partDetails.getObjectNumber());
                        //构建全局替代件
                        JSONObject partJson = JSONObject.parseObject(JSONObject.toJSONString(partDetails));
                        Map<String, Object> partKeyValue = new HashMap<>();
                        String main_plm_source_type_id = null;

                        for (Map<String, Object> option : options) {
                            if ("类型属性".equals(option.get("plmtype"))) {
                                main_plm_source_type_id = (String) option.get("plm_source_type_id");
                                k3CloudHttpService.setInTree(
                                        (List<Map<String, Object>>) inMap.get(option.get("plm_source_type_id")),
                                        partJson, partKeyValue);
                                break;
                            }
                        }

                        if (bomKeyValue.containsKey(main_plm_source_type_id)) {
                            List<Map<String, Object>> bomList = (List<Map<String, Object>>) bomKeyValue.get(
                                    main_plm_source_type_id);
                            bomList.add(partKeyValue);
                        } else {
                            List<Map<String, Object>> bomList = new ArrayList<>();
                            bomList.add(partKeyValue);
                            bomKeyValue.put(main_plm_source_type_id, bomList);
                        }
                        bomKeyValue.putAll(partKeyValue);
                        List<String> linkIdList = new ArrayList<>();
                        for (int a = 0; a < listAlternates.size(); a++) {
                            AlternateInfoVO alternateInfoVO = listAlternates.get(a);

                            linkIdList.add(alternateInfoVO.getId());

                            JSONObject alternateInfoJson = JSONObject.parseObject(
                                    JSONObject.toJSONString(alternateInfoVO));
                            Map<String, Object> listKeyValue = new HashMap<>();
                            String plm_source_type_id = null;
                            for (Map<String, Object> option : options) {
                                if ("全局替代关系属性".equals(option.get("plmtype"))) {
                                    plm_source_type_id = (String) option.get("plm_source_type_id");
                                    k3CloudHttpService.setInTree(
                                            (List<Map<String, Object>>) inMap.get(option.get("plm_source_type_id")),
                                            alternateInfoJson, listKeyValue);
                                    break;
                                }
                            }
                            if (bomKeyValue.containsKey(plm_source_type_id)) {
                                List<Map<String, Object>> bomList = (List<Map<String, Object>>) bomKeyValue.get(
                                        plm_source_type_id);
                                bomList.add(listKeyValue);
                            } else {
                                List<Map<String, Object>> bomList = new ArrayList<>();
                                bomList.add(listKeyValue);
                                bomKeyValue.put(plm_source_type_id, bomList);
                            }
                        }
                        Map<String, Object> out = k3CloudHttpService.plmToK3CloudOut(code, k3cloud, new HashMap<>(),
                                                                                     bomKeyValue);

                        if (out.containsKey("Model")) {
                            Map<String, Object> Model = (Map<String, Object>) out.get("Model");
                            List<Map<String, Object>> FEntity = (List<Map<String, Object>>) Model.get("FEntity");
                            if (FEntity.size() > 0) {
                                for (int x = 0; x < FEntity.size(); x++) {
                                    //替代料优先级
                                    FEntity.get(x).put("FPriority", x + 1);
                                }
                            } else {
                                //不需要修改替代件
                                continue;
                            }
                        }


                        executionType.put("objectNumber", partDetails.getObjectNumber());
                        executionType.put("plm_code", partDetails.getIdentifierDisplayName());

                        JSONObject resultJson = SendErpDataUtils.sendErpDataTaskWorkder(executionType, out);

                        JSONObject Result = resultJson.getJSONObject("Result");
                        if (Result != null) {
                            JSONObject ResponseStatus = Result.getJSONObject("ResponseStatus");
                            if (ResponseStatus.getBooleanValue("IsSuccess") == false) {
                                throw SystemErrorCodeEnum.WORK_FLOW_CONTENT_ERROR.getException(
                                        ResponseStatus.get("Errors").toString());
                            } else {
                                JSONArray successEntitys = ResponseStatus.getJSONArray("SuccessEntitys");
                                if (successEntitys.size() > 0) {
                                    //修改PLM全局替代方案编码
                                    List<String> codeList = new ArrayList<>();
                                    for (int a = 0; a < successEntitys.size(); a++) {
                                        String erp_process = successEntitys.getJSONObject(a).getString("Number");
                                        codeList.add(erp_process);
                                        plmNumbers.add(erp_process);
                                    }
                                    for (int a = 0; a < linkIdList.size(); a++) {
                                        TyplmUpdateLinkIbaDTO typlmUpdateLinkIbaDTO = new TyplmUpdateLinkIbaDTO();
                                        typlmUpdateLinkIbaDTO.setLinkId(linkIdList.get(a));
                                        Map<String, List<String>> ibaAttributeMap = new HashMap<>();
                                        ibaAttributeMap.put("替代方案编码", codeList);
                                        typlmUpdateLinkIbaDTO.setIbaAttributeMap(ibaAttributeMap);

                                        List<TyplmUpdateLinkIbaDTO> typlmUpdateLinkIbaList = new ArrayList<>();
                                        typlmUpdateLinkIbaList.add(typlmUpdateLinkIbaDTO);
                                        hzTyplmPdmPartAlternateService.batchUpdatePartAlterLinkIba(
                                                typlmUpdateLinkIbaList);
                                    }
                                }
                            }
                        }
                    }

                }
                if (listSubstitute != null && listSubstitute.size() > 0) {
                    //判断是否需要修改替代件
                    boolean isUpdate = false;
                    for (int a = 0; a < listSubstitute.size(); a++) {
                        PartSubstituteVO partSubstituteVO = listSubstitute.get(a);

                        Map<String, List<String>> allTd = partSubstituteVO.getIbaAttributeMap();
                        if (allTd != null) {
                            if (allTd.containsKey("替代方案编码")) {
                                List<String> cList = allTd.get("替代方案编码");
                                if (cList.size() == 0 || "".equals(cList.get(0))) {
                                    isUpdate = true;
                                    break;
                                }
                            }
                        } else {
                            isUpdate = true;
                        }
                    }
                    if (isUpdate) {
                        Map<String, Object> bomKeyValue = new HashMap<>();
                        JSONObject partJson = JSONObject.parseObject(JSONObject.toJSONString(partDetails));

                        List<String> removeList = new ArrayList<>();
                        removeList.add(partDetails.getObjectNumber());

                        Map<String, Object> partKeyValue = new HashMap<>();
                        String main_plm_source_type_id = null;

                        for (Map<String, Object> option : options) {
                            if ("类型属性".equals(option.get("plmtype"))) {
                                main_plm_source_type_id = (String) option.get("plm_source_type_id");
                                k3CloudHttpService.setInTree(
                                        (List<Map<String, Object>>) inMap.get(option.get("plm_source_type_id")),
                                        partJson, partKeyValue);
                                break;
                            }
                        }
                        if (bomKeyValue.containsKey(main_plm_source_type_id)) {
                            List<Map<String, Object>> bomList = (List<Map<String, Object>>) bomKeyValue.get(
                                    main_plm_source_type_id);
                            bomList.add(partKeyValue);
                        } else {
                            List<Map<String, Object>> bomList = new ArrayList<>();
                            bomList.add(partKeyValue);
                            bomKeyValue.put(main_plm_source_type_id, bomList);
                        }

                        bomKeyValue.putAll(partKeyValue);
                        //构建局部替代件

                        List<String> linkIdList = new ArrayList<>();

                        for (int a = 0; a < listSubstitute.size(); a++) {
                            PartSubstituteVO partSubstituteVO = listSubstitute.get(a);

                            linkIdList.add(partSubstituteVO.getLinkId());

                            JSONObject alternateInfoJson = JSONObject.parseObject(
                                    JSONObject.toJSONString(partSubstituteVO));
                            String plm_source_type_id = null;
                            Map<String, Object> listKeyValue = new HashMap<>();
                            for (Map<String, Object> option : options) {
                                if ("局部替代关系属性".equals(option.get("plmtype"))) {
                                    plm_source_type_id = (String) option.get("plm_source_type_id");
                                    k3CloudHttpService.setInTree(
                                            (List<Map<String, Object>>) inMap.get(option.get("plm_source_type_id")),
                                            alternateInfoJson, listKeyValue);
                                    break;
                                }
                            }

                            if (bomKeyValue.containsKey(plm_source_type_id)) {
                                List<Map<String, Object>> bomList = (List<Map<String, Object>>) bomKeyValue.get(
                                        plm_source_type_id);
                                bomList.add(listKeyValue);
                            } else {
                                List<Map<String, Object>> bomList = new ArrayList<>();
                                bomList.add(listKeyValue);
                                bomKeyValue.put(plm_source_type_id, bomList);
                            }
                        }
                        Map<String, Object> out = k3CloudHttpService.plmToK3CloudOut(code, k3cloud, new HashMap<>(),
                                                                                     bomKeyValue);
                        if (out.containsKey("Model")) {
                            Map<String, Object> Model = (Map<String, Object>) out.get("Model");
                            List<Map<String, Object>> FEntity = (List<Map<String, Object>>) Model.get("FEntity");
                            if (FEntity.size() > 0) {
                                for (int x = 0; x < FEntity.size(); x++) {
                                    FEntity.get(x).put("FPriority", x + 1);
                                }
                            } else {
                                //不需要修改替代件
                                continue;
                            }
                        }
                        executionType.put("objectNumber", partDetails.getObjectNumber());
                        executionType.put("plm_code", partDetails.getIdentifierDisplayName());

                        JSONObject resultJson = SendErpDataUtils.sendErpDataTaskWorkder(executionType, out);
                        if (resultJson != null) {
                            JSONObject Result = resultJson.getJSONObject("Result");
                            if (Result != null) {
                                JSONObject ResponseStatus = Result.getJSONObject("ResponseStatus");
                                if (ResponseStatus.getBooleanValue("IsSuccess") == false) {
                                    throw SystemErrorCodeEnum.WORK_FLOW_CONTENT_ERROR.getException(
                                            ResponseStatus.get("Errors").toString());
                                } else {
                                    JSONArray successEntitys = ResponseStatus.getJSONArray("SuccessEntitys");
                                    if (successEntitys.size() > 0) {
                                        //修改PLM局部替代方案编码
                                        List<String> codeList = new ArrayList<>();
                                        for (int a = 0; a < successEntitys.size(); a++) {
                                            String erp_process = successEntitys.getJSONObject(a).getString("Number");
                                            codeList.add(erp_process);
                                            plmNumbers.add(erp_process);
                                        }

                                        for (int a = 0; a < linkIdList.size(); a++) {
                                            List<TyplmUpdateLinkIbaDTO> typlmUpdateLinkIbaDTOList = new ArrayList<>();
                                            TyplmUpdateLinkIbaDTO typlmUpdateLinkIbaDTO = new TyplmUpdateLinkIbaDTO();
                                            typlmUpdateLinkIbaDTO.setLinkId(linkIdList.get(a));
                                            Map<String, List<String>> ibaAttributeMap = new HashMap<>();
                                            ibaAttributeMap.put("替代方案编码", codeList);
                                            typlmUpdateLinkIbaDTO.setIbaAttributeMap(ibaAttributeMap);
                                            typlmUpdateLinkIbaDTOList.add(typlmUpdateLinkIbaDTO);
                                            hzplmPdmPartSubstituteService.batchUpdatePartSubstituteLinkIba(
                                                    typlmUpdateLinkIbaDTOList);
                                        }
                                    }
                                }
                            }
                        }
                    }

                }
            }

            //审核替代关系
            executionType.put("code","TJ000006X");
            JSONObject resultJson = SubmissionAndReviewHanding.submittReview(executionType, plmNumbers);
            return resultJson;
        }


        return null;
    }


    public JSONObject productionCall(Map<String, Object> executionType, List<Map<String, Object>> alternativeList)
            throws Exception {

        ExtendtyplmPdmPartAlternateServiceImpl hzTyplmPdmPartAlternateService = SpringUtil.getBean(
                ExtendtyplmPdmPartAlternateServiceImpl.class);
        K3CloudHttpService k3CloudHttpService = SpringUtil.getBean(K3CloudHttpService.class);
        ExtendPlmPdmPartSubstituteServiceImpl hzplmPdmPartSubstituteService = SpringUtil.getBean(
                ExtendPlmPdmPartSubstituteServiceImpl.class);

        String k3cloud = (String) executionType.get("k3cloud");
        String code = (String) executionType.get("code");

        //创建替代关系
        List<Map<String, Object>> options = k3CloudHttpService.getOptions(code, k3cloud);
        Map<String, Object> inMap = k3CloudHttpService.getInTreeMap(code, k3cloud);
        List<String> plmNumbers = new ArrayList<>();

        for (int i = 0; i < alternativeList.size(); i++) {
            Map<String, Object> alternative = alternativeList.get(i);
            List<AlternateInfoVO> listAlternates = (List<AlternateInfoVO>) alternative.get("listAlternates");
            List<PartSubstituteVO> listSubstitute = (List<PartSubstituteVO>) alternative.get("listSubstitute");
            PartDetailsVO partDetails = (PartDetailsVO) alternative.get("partDetails");

            if (listAlternates != null && listAlternates.size() > 0) {
                //判断是否需要修改替代件
                boolean isUpdate = true;

                if (isUpdate) {
                    Map<String, Object> bomKeyValue = new HashMap<>();
                    List<String> removeList = new ArrayList<>();
                    removeList.add(partDetails.getObjectNumber());
                    //构建全局替代件
                    JSONObject partJson = JSONObject.parseObject(JSONObject.toJSONString(partDetails));
                    Map<String, Object> partKeyValue = new HashMap<>();
                    String main_plm_source_type_id = null;

                    for (Map<String, Object> option : options) {
                        if ("类型属性".equals(option.get("plmtype"))) {
                            main_plm_source_type_id = (String) option.get("plm_source_type_id");
                            k3CloudHttpService.setInTree(
                                    (List<Map<String, Object>>) inMap.get(option.get("plm_source_type_id")), partJson,
                                    partKeyValue);
                            break;
                        }
                    }

                    if (bomKeyValue.containsKey(main_plm_source_type_id)) {
                        List<Map<String, Object>> bomList = (List<Map<String, Object>>) bomKeyValue.get(
                                main_plm_source_type_id);
                        bomList.add(partKeyValue);
                    } else {
                        List<Map<String, Object>> bomList = new ArrayList<>();
                        bomList.add(partKeyValue);
                        bomKeyValue.put(main_plm_source_type_id, bomList);
                    }
                    bomKeyValue.putAll(partKeyValue);
                    List<String> linkIdList = new ArrayList<>();
                    for (int a = 0; a < listAlternates.size(); a++) {
                        AlternateInfoVO alternateInfoVO = listAlternates.get(a);

                        linkIdList.add(alternateInfoVO.getId());

                        JSONObject alternateInfoJson = JSONObject.parseObject(JSONObject.toJSONString(alternateInfoVO));
                        Map<String, Object> listKeyValue = new HashMap<>();
                        String plm_source_type_id = null;
                        for (Map<String, Object> option : options) {
                            if ("全局替代关系属性".equals(option.get("plmtype"))) {
                                plm_source_type_id = (String) option.get("plm_source_type_id");
                                k3CloudHttpService.setInTree(
                                        (List<Map<String, Object>>) inMap.get(option.get("plm_source_type_id")),
                                        alternateInfoJson, listKeyValue);
                                break;
                            }
                        }
                        if (bomKeyValue.containsKey(plm_source_type_id)) {
                            List<Map<String, Object>> bomList = (List<Map<String, Object>>) bomKeyValue.get(
                                    plm_source_type_id);
                            bomList.add(listKeyValue);
                        } else {
                            List<Map<String, Object>> bomList = new ArrayList<>();
                            bomList.add(listKeyValue);
                            bomKeyValue.put(plm_source_type_id, bomList);
                        }
                    }
                    Map<String, Object> out = k3CloudHttpService.plmToK3CloudOut(code, k3cloud, new HashMap<>(),
                                                                                 bomKeyValue);

                    if (out.containsKey("Model")) {
                        Map<String, Object> Model = (Map<String, Object>) out.get("Model");
                        List<Map<String, Object>> FEntity = (List<Map<String, Object>>) Model.get("FEntity");
                        if (FEntity.size() > 0) {
                            for (int x = 0; x < FEntity.size(); x++) {
                                //替代料优先级
                                FEntity.get(x).put("FPriority", x + 1);
                            }
                        } else {
                            //不需要修改替代件
                            continue;
                        }
                    }


                    executionType.put("objectNumber", partDetails.getObjectNumber());
                    JSONObject resultJson = SendErpDataUtils.sendErpDataTaskWorkder(executionType, out);

                    JSONObject Result = resultJson.getJSONObject("Result");
                    if (Result != null) {
                        JSONObject ResponseStatus = Result.getJSONObject("ResponseStatus");
                        if (ResponseStatus.getBooleanValue("IsSuccess") == false) {
                            throw SystemErrorCodeEnum.WORK_FLOW_CONTENT_ERROR.getException(
                                    ResponseStatus.get("Errors").toString());
                        } else {
                            JSONArray successEntitys = ResponseStatus.getJSONArray("SuccessEntitys");
                            if (successEntitys.size() > 0) {
                                //修改PLM全局替代方案编码
                                List<String> codeList = new ArrayList<>();
                                for (int a = 0; a < successEntitys.size(); a++) {
                                    String erp_process = successEntitys.getJSONObject(a).getString("Number");
                                    codeList.add(erp_process);
                                    plmNumbers.add(erp_process);
                                }
                                for (int a = 0; a < linkIdList.size(); a++) {
                                    TyplmUpdateLinkIbaDTO typlmUpdateLinkIbaDTO = new TyplmUpdateLinkIbaDTO();
                                    typlmUpdateLinkIbaDTO.setLinkId(linkIdList.get(a));
                                    Map<String, List<String>> ibaAttributeMap = new HashMap<>();
                                    ibaAttributeMap.put("替代方案编码", codeList);
                                    typlmUpdateLinkIbaDTO.setIbaAttributeMap(ibaAttributeMap);

                                    List<TyplmUpdateLinkIbaDTO> typlmUpdateLinkIbaList = new ArrayList<>();
                                    typlmUpdateLinkIbaList.add(typlmUpdateLinkIbaDTO);
                                    hzTyplmPdmPartAlternateService.batchUpdatePartAlterLinkIba(typlmUpdateLinkIbaList);
                                }
                            }
                        }
                    }
                }

            }
            if (listSubstitute != null && listSubstitute.size() > 0) {
                //判断是否需要修改替代件
                boolean isUpdate = true;
                if (isUpdate) {
                    Map<String, Object> bomKeyValue = new HashMap<>();
                    JSONObject partJson = JSONObject.parseObject(JSONObject.toJSONString(partDetails));

                    List<String> removeList = new ArrayList<>();
                    removeList.add(partDetails.getObjectNumber());

                    Map<String, Object> partKeyValue = new HashMap<>();
                    String main_plm_source_type_id = null;

                    for (Map<String, Object> option : options) {
                        if ("类型属性".equals(option.get("plmtype"))) {
                            main_plm_source_type_id = (String) option.get("plm_source_type_id");
                            k3CloudHttpService.setInTree(
                                    (List<Map<String, Object>>) inMap.get(option.get("plm_source_type_id")), partJson,
                                    partKeyValue);
                            break;
                        }
                    }
                    if (bomKeyValue.containsKey(main_plm_source_type_id)) {
                        List<Map<String, Object>> bomList = (List<Map<String, Object>>) bomKeyValue.get(
                                main_plm_source_type_id);
                        bomList.add(partKeyValue);
                    } else {
                        List<Map<String, Object>> bomList = new ArrayList<>();
                        bomList.add(partKeyValue);
                        bomKeyValue.put(main_plm_source_type_id, bomList);
                    }

                    bomKeyValue.putAll(partKeyValue);
                    //构建局部替代件

                    List<String> linkIdList = new ArrayList<>();

                    for (int a = 0; a < listSubstitute.size(); a++) {
                        PartSubstituteVO partSubstituteVO = listSubstitute.get(a);

                        linkIdList.add(partSubstituteVO.getLinkId());

                        JSONObject alternateInfoJson = JSONObject.parseObject(
                                JSONObject.toJSONString(partSubstituteVO));
                        String plm_source_type_id = null;
                        Map<String, Object> listKeyValue = new HashMap<>();
                        for (Map<String, Object> option : options) {
                            if ("局部替代关系属性".equals(option.get("plmtype"))) {
                                plm_source_type_id = (String) option.get("plm_source_type_id");
                                k3CloudHttpService.setInTree(
                                        (List<Map<String, Object>>) inMap.get(option.get("plm_source_type_id")),
                                        alternateInfoJson, listKeyValue);
                                break;
                            }
                        }

                        if (bomKeyValue.containsKey(plm_source_type_id)) {
                            List<Map<String, Object>> bomList = (List<Map<String, Object>>) bomKeyValue.get(
                                    plm_source_type_id);
                            bomList.add(listKeyValue);
                        } else {
                            List<Map<String, Object>> bomList = new ArrayList<>();
                            bomList.add(listKeyValue);
                            bomKeyValue.put(plm_source_type_id, bomList);
                        }
                    }
                    Map<String, Object> out = k3CloudHttpService.plmToK3CloudOut(code, k3cloud, new HashMap<>(),
                                                                                 bomKeyValue);
                    if (out.containsKey("Model")) {
                        Map<String, Object> Model = (Map<String, Object>) out.get("Model");
                        List<Map<String, Object>> FEntity = (List<Map<String, Object>>) Model.get("FEntity");
                        if (FEntity.size() > 0) {
                            for (int x = 0; x < FEntity.size(); x++) {
                                FEntity.get(x).put("FPriority", x + 1);
                            }
                        } else {
                            //不需要修改替代件
                            continue;
                        }
                    }
                    executionType.put("objectNumber", partDetails.getObjectNumber());
                    JSONObject resultJson = SendErpDataUtils.sendErpDataTaskWorkder(executionType, out);
                    if (resultJson != null) {
                        JSONObject Result = resultJson.getJSONObject("Result");
                        if (Result != null) {
                            JSONObject ResponseStatus = Result.getJSONObject("ResponseStatus");
                            if (ResponseStatus.getBooleanValue("IsSuccess") == false) {
                                throw SystemErrorCodeEnum.WORK_FLOW_CONTENT_ERROR.getException(
                                        ResponseStatus.get("Errors").toString());
                            } else {
                                JSONArray successEntitys = ResponseStatus.getJSONArray("SuccessEntitys");
                                if (successEntitys.size() > 0) {
                                    //修改PLM局部替代方案编码
                                    List<String> codeList = new ArrayList<>();
                                    for (int a = 0; a < successEntitys.size(); a++) {
                                        String erp_process = successEntitys.getJSONObject(a).getString("Number");
                                        codeList.add(erp_process);
                                        plmNumbers.add(erp_process);
                                    }

                                    for (int a = 0; a < linkIdList.size(); a++) {
                                        List<TyplmUpdateLinkIbaDTO> typlmUpdateLinkIbaDTOList = new ArrayList<>();
                                        TyplmUpdateLinkIbaDTO typlmUpdateLinkIbaDTO = new TyplmUpdateLinkIbaDTO();
                                        typlmUpdateLinkIbaDTO.setLinkId(linkIdList.get(a));
                                        Map<String, List<String>> ibaAttributeMap = new HashMap<>();
                                        ibaAttributeMap.put("替代方案编码", codeList);
                                        typlmUpdateLinkIbaDTO.setIbaAttributeMap(ibaAttributeMap);
                                        typlmUpdateLinkIbaDTOList.add(typlmUpdateLinkIbaDTO);
                                        hzplmPdmPartSubstituteService.batchUpdatePartSubstituteLinkIba(
                                                typlmUpdateLinkIbaDTOList);
                                    }
                                }
                            }
                        }
                    }
                }

            }
        }

        //审核替代关系
        executionType.put("code","TJ000006X");
        JSONObject resultJson = SubmissionAndReviewHanding.submittReview(executionType, plmNumbers);
        return resultJson;
    }
}
